﻿using JinJun.YShow.BLL;
using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using JinJun.YShow.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Controllers
{
    public class ProductController : BaseController
    {
        /*
        只定义了当前表的业务层，如需其他自行添加构造接口
           */

        private IProductService _productService;
        private IPictureService _pictureService;
        private ICategoryService _categoryService;

        public ProductController(IProductService productService, IPictureService pictureService, ICategoryService categoryService)
        {
            _productService = productService;
            _pictureService = pictureService;
            _categoryService = categoryService;
        }

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetProduct(int page, int limit)
        {
            //定义表数据总数变量
            int count = 0;
            Expression<Func<Product_Spu, bool>> lambdaCategory = a => true;
            Expression<Func<Product_Spu, int>> orderCategory = a => a.Id;
            //创建List集合接收业务层返回值
            List<Product_Spu> product_Spus = _productService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //创建PageListResult对象
            PageListResult<Product_Spu> pageListResult = new PageListResult<Product_Spu>();
            //框架属性赋值
            pageListResult.code = 0;
            pageListResult.count = count;
            pageListResult.msg = "";
            pageListResult.data = product_Spus;
            //返回PageListResult对象
            return Json(pageListResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 修改上架状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdatePublishStatus(int id, short states)
        {
            Expression<Func<Product_Spu, bool>> lambdaCategory = a => a.Id == id;
            Product_Spu pic = _productService.Query(lambdaCategory);
            pic.Publish_Status = states;
            pic.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _productService.Update(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 修改审核状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdateAuditStatus(int id, short states)
        {
            Expression<Func<Product_Spu, bool>> lambdaCategory = a => a.Id == id;
            Product_Spu pic = _productService.Query(lambdaCategory);
            pic.Audit_Status = states;
            pic.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _productService.Update(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public JsonResult DeleteProduct(Product_Spu product_Spu)
        {
            OperateResult operateResult = new OperateResult();
            Expression<Func<Pic, bool>> lambdaCategory = a =>a.ProductId== product_Spu.Id;
            Pic pic = _pictureService.Query(lambdaCategory);
            if (pic != null)
            {
                operateResult.Success = _productService.DeleteProduct(product_Spu, pic);
            }
            else
            {
                operateResult.Success = _productService.Delete(product_Spu);
            }
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 添加规格
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult AddSpce(Spec spec)
        {
            BaseService<Spec> baseService = new BaseService<Spec>();
            spec.CreateTime = DateTime.Now;
            spec.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = baseService.Add(spec);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 添加规格值
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult AddSpceValue(Spec_Value spec_Value)
        {
            BaseService<Spec_Value> baseService = new BaseService<Spec_Value>();
            spec_Value.CreateTime = DateTime.Now;
            spec_Value.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = baseService.Add(spec_Value);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult AddProduct(ProductSpce productSpce)
        {
            OperateResult operateResult = new OperateResult();
            //获取商品对象
            Product_Spu product_Spu = productSpce.product_Spu;
            product_Spu.InDate = DateTime.Now;
            product_Spu.ModifiedTime = DateTime.Now;
            //获取规格集合
            List<Spec> specs = productSpce.specs;
            //调用添加方法
            operateResult.Success = _productService.BatchAdd(product_Spu, specs);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 获取下级菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult GetSonMenu(Category category)
        {
            Expression<Func<Category, bool>> lambdaCategory = a => a.ParentId == category.Id;
            List<Category> categories= _categoryService.QueryList(lambdaCategory);
            return Json(categories, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 获取下级菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult GetSpecValue(Spec spec)
        {
            BaseService<Spec_Value> baseService = new BaseService<Spec_Value>();
            Expression<Func<Spec_Value, bool>> lambdaCategory = a => a.Spec_Id == spec.Id;
            List<Spec_Value> categories = baseService.QueryList(lambdaCategory);
            return Json(categories, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 获取下级菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult EditStock(ProducSku producSku)
        {
            OperateResult operateResult = new OperateResult();
            Product_Sku product_Sku = new Product_Sku();
            Produc_Sku produc_Sku = new Produc_Sku();
            //商品sku表赋值
            product_Sku.CreateTime = DateTime.Now;
            product_Sku.ModifiedTime = DateTime.Now;
            product_Sku.ProductId = producSku.ProductId;
            product_Sku.Spec_Id = producSku.Spec_Id;
            product_Sku.Stock = producSku.Stock;
            //商品sku规格值关联表赋值
            produc_Sku.ModifiedTime = DateTime.Now;
            produc_Sku.CreateTime = DateTime.Now;
            produc_Sku.Spec_Value_Id = producSku.Spec_Value_Id;
            //调用设置库存方法
            operateResult.Success=_productService.EditStock(product_Sku, produc_Sku);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 编辑商品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult EditProdusts(ProductSpce productSpce)
        {
            //记录不匹配的规格Id
            List<Product_Sku> product_Skus = new List<Product_Sku>();
            
            OperateResult operateResult = new OperateResult();
            //获取商品对象--用于修改
            Product_Spu product_Spu = productSpce.product_Spu;
            product_Spu.InDate = DateTime.Now;
            product_Spu.ModifiedTime = DateTime.Now;
            //获取规格集合--用于新增
            List<Spec> specs = productSpce.specs;
            //定义查询当前商品spu规格关联表的表达式--用于删除
            Expression<Func<Produc_Spu_Spec, bool>> lambdaCategory3 = a => a.Produc_Spu_Id == product_Spu.Id;
            BaseService<Produc_Spu_Spec> baseService1 = new BaseService<Produc_Spu_Spec>();
            //获取当前商品的spu规格表集合--用于删除
            List<Produc_Spu_Spec> produc_Spu_Specs = baseService1.QueryList(lambdaCategory3);
            //查询商品sku表的表达式
            Expression<Func<Product_Sku, bool>> lambdaCategory = a => a.ProductId == product_Spu.Id;
            BaseService<Product_Sku> baseService = new BaseService<Product_Sku>();
            //获取当前商品sku表的集合
            List<Product_Sku> product_Skus1 = baseService.QueryList(lambdaCategory);
            //循环遍历当前商品sku表的集合
            for (int i = 0; i < specs.Count; i++)
            {
                for (int k = 0; k < product_Skus1.Count; k++)
                {
                    //判断是否有新增或减少规格，若有则加入集合中--用于删除
                    if (specs[i].Id == product_Skus1[k].Spec_Id)
                    {
                        break;
                    }
                    else if(k== product_Skus1.Count-1)
                    {
                        product_Skus.Add(product_Skus1[k]);
                    }
                }
            }
            BaseService<Produc_Sku> baseService2 = new BaseService<Produc_Sku>();
            //定义商品sku规格值关联表集合
            List<Produc_Sku> produc_Skus = new List<Produc_Sku>();
            //判断记录不匹配集合长度，确定是否有新增或减少规格
            if (product_Skus.Count != 0)
            {
                //遍历将商品sku规格值关联表的不匹配的添加进集合
                for (int i = 0; i < product_Skus.Count; i++)
                {
                    //根据不匹配集合的每个对象Id查询出对应的商品sku规格值关联表对象，并添加进集合中---用于删除
                    int skid = product_Skus[i].Id;
                    Expression<Func<Produc_Sku, bool>> lambdaCategory2 = a => a.Product_Sku_Id == skid;
                    produc_Skus.Add(baseService2.Query(lambdaCategory2));
                }
                operateResult.Success = _productService.BatchEdit(product_Spu, specs, produc_Spu_Specs, product_Skus, produc_Skus);
                return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
            }
            //调用编辑方法
            operateResult.Success = _productService.BatchEdit(product_Spu, specs, produc_Spu_Specs);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

    }
}